package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.BindMyThirdReq;
import sz.szsmk.citizencard.model.network.bean.request.BindThirdReq;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;
import sz.szsmk.citizencard.model.network.bean.request.UnbindReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.BindThirdRes;
import sz.szsmk.citizencard.model.network.bean.response.OauthTokenRes;
import sz.szsmk.citizencard.mvp.contract.IBindContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/4 13:27
 * description:
 */
public class BindListPresenter extends IBasePresenter implements
        IBindContact.IBindPresenter<IBindContact.IBindView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public BindListPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getBindList() {
        BindThirdReq bindThirdReq = new BindThirdReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        bindThirdReq.setAccess_token(access_token);
        bindThirdReq.setUser_type(0);
        mNetworkDateSource.getBindThirdApp(bindThirdReq, new SubscribeResult<BindThirdRes>() {
            @Override
            protected void onOk(BindThirdRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "getBindThirdApp---->" + json);
                List<BindThirdRes.ThirdAppUserExBean> thirdAppUserEx = response.getThirdAppUserEx();
                if (thirdAppUserEx != null) {
                    ((IBindContact.IBindView) mView).getBindListSucc(thirdAppUserEx);
                }
            }

            @Override
            protected void onFailure(BindThirdRes response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void oauthToken() {
        IBaseRequest baseRequest = new IBaseRequest();
        baseRequest.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        mNetworkDateSource.oauthToken(baseRequest, new SubscribeResult<OauthTokenRes>() {
            @Override
            protected void onOk(OauthTokenRes response) {
                ((IBindContact.IBindView) mView).oauthTokenSucc(response);
            }

            @Override
            protected void onFailure(OauthTokenRes response) {
                super.onFailure(response);

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void bindThird(String login_type, String uid, String access_token) {
        BindMyThirdReq bindMyThirdReq = new BindMyThirdReq();
        bindMyThirdReq.setAccess_token(access_token);
        bindMyThirdReq.setLogin_type(login_type);
        bindMyThirdReq.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        bindMyThirdReq.setUid(uid);
        String json = new Gson().toJson(bindMyThirdReq);
        L.e(Constant.TAG,"bindMyThirdReq----->"+json);
        mNetworkDateSource.userBindthirdApp(bindMyThirdReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IBindContact.IBindView) mView).bindThirdSucc();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IBindContact.IBindView) mView).bindThirdFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IBindContact.IBindView) mView).bindThirdFail();
            }
        });


    }

    @Override
    public void unBind(int loginType) {
        UnbindReq unbindReq = new UnbindReq();
        unbindReq.setLogin_type(loginType);
        mNetworkDateSource.unBindThirdAppUser(unbindReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IBindContact.IBindView) mView).unBindSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IBindContact.IBindView) mView).unBindFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
